package fieldmaps.voronoi;

import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JFrame;
import javax.vecmath.Point2d;

import com.vividsolutions.jts.geom.Polygon;

import fieldmaps.representation.GenericMapRepresentation;
import fieldmaps.voronoiLibrary.DelaunayAp;

@SuppressWarnings("deprecation")
public class VoronoiFormatRepresentation {
	private List<VoronoiCellRepresentation> voronoiCells;
	
	public VoronoiFormatRepresentation() {
		super();
		this.voronoiCells = new LinkedList<VoronoiCellRepresentation>();
	}

	public void loadCoordinatesFile (String fileName) throws IOException, IllegalArgumentException 
	{
		FileInputStream file = new FileInputStream(fileName);
		InputStreamReader stream = new InputStreamReader(file);
		BufferedReader reader = new BufferedReader(stream);
		String line = null;

		while ((line = reader.readLine()) != null) {
			String[] values = line.split(" ");
			if(values.length > 1) {
				voronoiCells.add(new VoronoiCellRepresentation(Double.valueOf(values[0]), Double.valueOf(values[1])));
			}
		}
	}
	
	private void printCoordinates() {
		for( VoronoiCellRepresentation v : voronoiCells )	{
			System.out.println( v.getPoint().x + " " + v.getPoint().y );
		}	
	}
	
	private void viewVoronoiDiagram()
	{
		DelaunayAp applet = new DelaunayAp();    // Create applet
        applet.init();                           // Applet initialization
        
        List<Point2d> points = new ArrayList<Point2d>();
        for( VoronoiCellRepresentation v : voronoiCells )	{
        	points.add(v.getPoint());
        }
        
        applet.addPoints(points);
        
        JFrame dWindow = new JFrame();           // Create window
        dWindow.setSize(700, 500);               // Set window size
        dWindow.setTitle("Voronoi/Delaunay Window");           // Set window title
        dWindow.setLayout(new BorderLayout());   // Specify layout manager
        dWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                                 // Specify closing behavior
        dWindow.add(applet, "Center");           // Place applet into window
        dWindow.setVisible(true);                // Show the window
        
        
	}
	
	public static void main(String[] args) {

		String fileName = "dataGeneric/coordinates.txt";

		VoronoiFormatRepresentation voronoiFormatRepresentation = new VoronoiFormatRepresentation();
		
		try {
			voronoiFormatRepresentation.loadCoordinatesFile(fileName);
		} catch (FileNotFoundException e) {
			System.err.println("The file doesn't exists.");
			return;
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
		
		voronoiFormatRepresentation.printCoordinates();
		
		voronoiFormatRepresentation.viewVoronoiDiagram();
	}

	public Polygon getGeometryContainingPoint(java.awt.geom.Point2D.Double point) {
		// TODO Auto-generated method stub
		return null;
	}
}
